package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

/**
 * 正常情况下如果一个方法里面代码行数超过80行，都应该提取方法，提炼代码
 * 一个方法里面只做一件事情
 */
public class CompanyServiceImpl implements CompanyService {
    private CompanyDao companyDao = new CompanyImpl();

    @Override
    public CompanyModel getCompanyModel() {
        CompanyModel companyModel = new CompanyModel();
//        1.判断redis里面是否有数据 伪代码
//        如果要取得redis里面的数据，必须要得到redis对象
        Jedis jedis = new Jedis();
        boolean isHave = jedis.exists("logo");
        if (isHave) {
            //        2.如果有，则取redis里面的数据
//            companyModel=jedis.get("companyModel");
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setLinePhone(jedis.get("linePhone"));
            companyModel.setVersion(jedis.get("version"));

        } else {
            //        3.如果没有，则取数据库的数据
            companyModel = companyDao.getCompanyModel();
            //        4.讲数据库里面的数据保存到redis
//            redis.setData=companyModel;
//            jedis.set("companyModel",companyModel.toString());
//            jedis.set("logo", companyModel.getLogo());
//            jedis.expire("logo",1000);
            jedis.setex("logo",1000,companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("address", companyModel.getAddress());
            jedis.set("linePhone", companyModel.getLinePhone());
            jedis.set("version", companyModel.getVersion());

        }
//        5.最终讲数据封装成CompanyModel并返回
        return companyModel;
    }
}
